package admin

import (
	"beego/models"
	"beego/models/admin"
	"github.com/astaxie/beego"
	"strconv"
	"strings"
)

type RoleController struct {
	beego.Controller
}

func (c *RoleController) Get() {
	role := []admin.Role{}
	models.DB.Find(&role)

	c.Data["roleList"] = role
	c.Data["roleListLen"] = len(role)
	c.TplName = "admin/role/index.html"
}

func (c *RoleController) Add() {
	c.TplName = "admin/role/add.html"
}
func (c *RoleController) DoAdd() {
	name := c.GetString("name")
	descr := c.GetString("descr")
	role := admin.Role{}
	role.Name = name
	role.Descr = descr
	role.Ctime = int(models.GetUnix())
	err := models.DB.Create(&role).Error
	if err != nil {
		data := &JSONS{-1, "角色添加失败 请刷新后重试", []string{}}
		c.Data["json"] = data
		c.ServeJSON()
	}
	data := &JSONS{200, "添加成功", []string{}}
	c.Data["json"] = data
	c.ServeJSON()
}

func (c *RoleController) Edit() {
	id := c.GetString("id")
	role := admin.Role{}
	models.DB.Where("id = ?", id).First(&role)
	c.Data["role"] = role
	c.TplName = "admin/role/edit.html"
}

func (c *RoleController) DoEdit() {
	id, err := c.GetInt("id")
	if err != nil {
		beego.Info("id参数类型传输错误")
		return
	}
	name := strings.Trim(c.GetString("name"), " ")
	descr := strings.Trim(c.GetString("descr"), " ")
	role := admin.Role{Id: id}
	models.DB.Find(&role)
	role.Name = name
	role.Descr = descr
	err2 := models.DB.Save(&role).Error
	if err2 != nil {

		data := &JSONS{200, "角色修改失败", []string{}}
		c.Data["json"] = data
		c.ServeJSON()
	} else {
		data := &JSONS{200, "角色修改成功", []string{}}
		c.Data["json"] = data
		c.ServeJSON()
	}
}

func (c *RoleController) Delete() {
	id, err := c.GetInt("id")
	if err != nil {
		beego.Info("id参数类型传输错误")
		return
	}
	role := admin.Role{Id: id}
	err2 := models.DB.Delete(&role).Error
	if err2 != nil {
		data := &JSONS{200, "角色删除失败", []string{}}
		c.Data["json"] = data
		c.ServeJSON()
	}
	data := &JSONS{200, "角色删除成功", []string{}}
	c.Data["json"] = data
	c.ServeJSON()
}

//权限分配
func (c *RoleController) Role() {
	id, err := c.GetInt("id")
	if err != nil {
		data := &JSONS{-1, "角色分配失败，ID传值不正确", []string{}}
		c.Data["json"] = data
		c.ServeJSON()
	}
	//1		查询出来当前用户拥有的权限节点
	roleNode := make([]admin.RoleNode, 0)
	models.DB.Where("role_id = ?", id).Find(&roleNode)
	//1.1	把当前用户拥有的节点的id  放到map对象里
	nodeAccessMap := make(map[int]int)
	for _, v := range roleNode {
		nodeAccessMap[v.NodeId] = v.NodeId
	}

	//2		查询出来所有的权限节点
	allNode := make([]admin.Node, 0)
	models.DB.Preload("NodeItem").Where("pid = ?", 0).Find(&allNode)
	//2.1	给拥有的节点添加checked属性
	for i := 0; i < len(allNode); i++ {
		if _, ok := nodeAccessMap[allNode[i].Id]; ok {
			allNode[i].Checked = true
		}
		for j := 0; j < len(allNode[i].NodeItem); j++ {
			if _, ok := nodeAccessMap[allNode[i].NodeItem[j].Id]; ok {
				allNode[i].NodeItem[j].Checked = true
			}
		}
	}
	//c.Data["json"] = allNode
	//c.ServeJSON()
	c.Data["allNode"] = allNode
	c.Data["roleId"] = id
	c.TplName = "admin/role/role.html"
}

func (c *RoleController) DoRole() {
	roleId, err := c.GetInt("role_id")
	if err != nil {
		data := &JSONS{-1, "节点分配失败，ID传值不正确", []string{}}
		c.Data["json"] = data
		c.ServeJSON()
	}
	node_ids := c.GetStrings("node_ids")
	if node_ids == nil {
		data := &JSONS{-1, "权限分配为空", []string{}}
		c.Data["json"] = data
		c.ServeJSON()
	}
	//删除掉原有的节点
	roleNode := admin.RoleNode{}
	if err := models.DB.Where("role_id = ?", roleId).Delete(&roleNode).Error; err != nil {
		data := &JSONS{-1, "节点分配失败,请重新分配!", []string{}}
		c.Data["json"] = data
		c.ServeJSON()
	}
	//重新分配节点
	for _, value := range node_ids {
		roleNode := admin.RoleNode{}
		nId, _ := strconv.Atoi(value)
		roleNode.RoleId = roleId
		roleNode.NodeId = nId
		roleNode.Ctime = int(models.GetUnix())
		if err := models.DB.Create(&roleNode).Error; err != nil {
			data := &JSONS{-1, "节点分配失败,请重新分配!", []string{}}
			c.Data["json"] = data
			c.ServeJSON()
		}
	}
	data := &JSONS{200, "节点分配成功!!", []string{}}
	c.Data["json"] = data
	c.ServeJSON()
}
